import requests
import re
import os
import shutil

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
}
homePage = 'https://nf.asoul-rec.com'
videoRule = r'</a><a href="(.*?)" class="item" size=.*?><i class="far fa-file-video"></i>'
audioRule = r'</a><a href="(.*?)" class="item" size=.*?><i class="far fa-file-audio"></i>'
fileRule = r'</a><a href="(.*?)" class="item" size=.*?><i class="far fa-file"></i>'
folderRule = r'</a><a href="(.*?)" class="item" size=.*?><i class="far fa-folder"></i>'
_4kurl = 'https://nf.asoul-rec.com/%E6%AD%8C%E8%88%9E%E5%88%87%E7%89%87/4K%E5%85%A8%E6%AD%8C%E8%88%9E%E5%88%87%E7%89%87%EF%BC%88%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89%E7%9B%B4%E6%92%AD%E7%9A%84%E6%AD%8C%E8%88%9E%E3%80%81%E9%9F%B3%E8%BD%A8%E6%A0%A1%E5%AF%B9%EF%BC%89/'
addDict = {'luboAdd': '/ASOUL-REC/2020.12.11 全团首播.mp4', 'PBFAdd': '/ASOUL-REC/PBF%E4%B9%A6%E7%AD%BE%E6%96%87%E4%BB%B6/2020.12.11 全团首播.pbf', 
          'SRTAdd': '/ASOUL-REC/SRT%E5%AD%97%E5%B9%95%E8%BD%B4/2020.12.11 全团首播.srt',
          'MP3Add': '/%E6%AD%8C%E8%88%9E%E5%88%87%E7%89%87/MP3%E7%9B%B4%E6%92%AD%E6%AD%8C%E6%9B%B2%E5%88%87%E7%89%87/2020.12.11 歌曲串烧.mp3', 
          'chaofenbianlvAdd': '/%E6%AD%8C%E8%88%9E%E5%88%87%E7%89%87/%E8%B6%85%E5%88%86%E8%BE%A8%E7%8E%87%E6%8F%92%E5%B8%A7%E5%88%87%E7%89%87/【切片】乃琳&贝拉 推销奶贝 好女人调戏坏女人被反杀 4K120.mp4', 
          }
urlDict = {'lubo': '/ASOUL-REC/', 'AAC': '/ASOUL-REC/AAC录播音轨/', 'ASS': '/ASOUL-REC/ASS弹幕文件/', 
           'PBF': '/ASOUL-REC/PBF书签文件/', 'SRT': '/ASOUL-REC/SRT字幕轴/', 'XML': '/ASOUL-REC/XML弹幕文件/', 
          'dyNailin': '/Dy短视频/乃琳Queen/', 'dyXiangwan': '/Dy短视频/向晚大魔王/', 'dyJiaran': '/Dy短视频/嘉然今天吃什么/', 
          'dyJiale': '/Dy短视频/珈乐Carol/', 'dyBeila': '/Dy短视频/贝拉☆kira/', 'dyWugehun': '/Dy短视频/五个魂儿呀/', 
          'MP3': '/歌舞切片/MP3直播歌曲切片/', 'MP3xiufu': '/歌舞切片/MP3直播歌曲切片/修复文物/',
          'chaofenbianlv': '/歌舞切片/超分辨率插帧切片/'}
lines = []

def mkdir():
    if (os.path.exists('OneDrive直链')):
        shutil.rmtree('.\OneDrive直链')
    os.mkdir(os.getcwd() + '\\OneDrive直链')
    if (os.path.exists('CloudFlare代理下载')):
        shutil.rmtree('.\CloudFlare代理下载')
    os.mkdir(os.getcwd() + '\\CloudFlare代理下载')
        
def autowrite(url, rule, fileName, addFlag, add=[]):
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    
    obj = re.compile(rule, re.S)
    result = obj.findall(response.text)

    if addFlag:
        result.pop(0)
        result.insert(0, add)

    with open('OneDrive直链\\' + fileName + '.txt', "w", encoding='utf-8')as f:
        for content in result:
            content = homePage + content + '?raw\n'
            f.write(content)
        f.close()
        
    with open('CloudFlare代理下载\\' + fileName + '.txt', "w", encoding='utf-8')as f:
        for content in result:
            content = homePage + content + '?raw&proxied\n'
            f.write(content)
        f.close()

def spider(url, rule):
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    
    obj = re.compile(rule, re.S)
    result = obj.findall(response.text)
    return result


if __name__ == '__main__':
    mkdir()
    
    autowrite(homePage + urlDict['lubo'], videoRule, '录播', 1, addDict['luboAdd'])
    autowrite(homePage + urlDict['AAC'], audioRule, 'AAC录播音轨', 0)
    autowrite(homePage + urlDict['ASS'], fileRule, 'ASS弹幕文件', 0)
    autowrite(homePage + urlDict['PBF'], fileRule, 'PBF书签文件', 1, addDict['PBFAdd'])
    autowrite(homePage + urlDict['SRT'], fileRule, 'SRT字幕轴', 1, addDict['SRTAdd'])
    autowrite(homePage + urlDict['XML'], fileRule, 'XML弹幕文件', 0)
    
    autowrite(homePage + urlDict['dyNailin'], videoRule, '乃琳抖音', 0)
    autowrite(homePage + urlDict['dyJiaran'], videoRule, '嘉然抖音', 0)
    autowrite(homePage + urlDict['dyXiangwan'], videoRule, '向晚抖音', 0)
    autowrite(homePage + urlDict['dyBeila'], videoRule, '贝拉抖音', 0)
    autowrite(homePage + urlDict['dyJiale'], videoRule, '珈乐抖音', 0)
    autowrite(homePage + urlDict['dyWugehun'], videoRule, '五个魂儿抖音', 0)
    
    autowrite(homePage + urlDict['MP3'], audioRule, 'MP3直播歌曲切片', 1, addDict['MP3Add'])
    autowrite(homePage + urlDict['MP3xiufu'], audioRule, 'MP3修复', 0)
    autowrite(homePage + urlDict['chaofenbianlv'], videoRule, '超分辨率插帧切片', 1, addDict['chaofenbianlvAdd'])
    
    month = spider(_4kurl, folderRule)
    for numMonth in range(len(month)):
        day = spider(homePage + month[numMonth], folderRule)
        for numDay in range(len(day)):
            dayResult = spider(homePage + day[numDay], videoRule)
            with open('OneDrive直链\\' + '4K全歌舞切片.txt', "a", encoding='utf-8')as f:
                for dayContent in dayResult:
                    dayContent = homePage + dayContent + '?raw\n'
                    f.write(dayContent)
                f.close()
    with open('OneDrive直链\\' + '4K全歌舞切片.txt', 'r', encoding='utf-8') as f:
        for line in f:
            line = line[0:-1] + '&proxied' + '\n'
            lines.append(line)
        f.close()
    with open('CloudFlare代理下载\\' + '4K全歌舞切片.txt', 'w', encoding='utf-8')as f:
        f.writelines(lines)
        f.close()            
    print('OK')